package com.wzdigit.wms.wms.service.order;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.asn.AsnRequest;
import com.wzdigit.wms.basic.client.request.asn.ReceiveReportDtlRequest;
import com.wzdigit.wms.basic.client.request.asn.ReceiveReportSelectRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.asnHeader.ReceiveReportDtlExt;
import com.wzdigit.wms.basic.client.response.asnHeader.ReceiveReportSNExt;
import com.wzdigit.wms.basic.client.response.asnHeader.ReceiveReportZhuExt;
import com.wzdigit.wms.basic.client.response.inv.TrsnReportExt;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnDtlGoods;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.asn.dto.AsnDto;
import com.wzdigit.wms.basic.domain.asn.dto.AsnSaleInDto;
import com.wzdigit.wms.basic.enums.*;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.common.entity.ExcelExportConfig;
import com.wzdigit.wms.common.utils.HttpExcel;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.common.utils.QueryInUtil;
import com.wzdigit.wms.wms.service.core.GoodsService;
import com.wzdigit.wms.wms.service.core.asn.AsnDtlGoodsService;
import com.wzdigit.wms.wms.service.core.asn.AsnDtlService;
import com.wzdigit.wms.wms.service.core.asn.AsnHeaderService;
import com.wzdigit.wms.wms.service.core.trsn.TrsnDtlService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 收货单 服务类
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@Service
public class AsnService {

    @Autowired
    private AsnDtlMapper asnDtlMapper;
    
    @Autowired
    private AsnDtlGoodsMapper asnDtlGoodsMapper;
    
    @Autowired
    private AsnDtlGoodsService asnDtlGoodsService;
    
    @Autowired
    private AsnHeaderService asnHeaderService;
    
    @Autowired
    private TrsnDtlService trsnDtlService;

    @Autowired
    private AsnDtlService asnDtlService;
    
    @Autowired
    private GoodsService goodsService;
    
    public static Map<String, String> asnTypeEnumMap = AsnTypeEnum.getEnumByDescription();
    public static Map<Integer, String> asnDtlStatusEnumMap = AsnDtlStatusEnum.getEnumByDescription();
    public static Map<Integer, String> goodsStatusEnumMap = GoodsStatusEnum.getEnumByDescription();
    
    public static String inStorageCode = TrsnNoteTypeEnum.IN_STORAGE.getCode();		//入库凭证
    public static String drawCode = TrsnNoteTypeEnum.DRAW.getCode();				//翻牌凭证
    public static int invUpdateFlag = TrsnDtlInvUpdateFlagEnum.IN.code;
    public static List<String> asnTypeList;    // 收货通知单类型

    static {
        asnTypeList = new ArrayList<String>(7);
        asnTypeList.add(AsnTypeEnum.R01.code);
        asnTypeList.add(AsnTypeEnum.R04.code);
        asnTypeList.add(AsnTypeEnum.R08.code);
        asnTypeList.add(AsnTypeEnum.R09.code);
        asnTypeList.add(AsnTypeEnum.R10.code);
        asnTypeList.add(AsnTypeEnum.R96.code);
        asnTypeList.add(AsnTypeEnum.R97.code);
        asnTypeList.add(AsnTypeEnum.R98.code);
        asnTypeList.add(AsnTypeEnum.R99.code);
    }


    /**
     * 生成收货单、收货单明细、收货单明细货品清单(可选)
     * @param asnRequest
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Result<AsnRequest> addAsn(AsnRequest asnRequest, HttpServletRequest servletRequest, HttpServletResponse servletResponse) {

        //添加记录：收货通知单货品清单
        AsnDtlGoods asnDtlGoods = asnRequest.getAsnDtlGoods();
        asnDtlGoods.setCreateTime(new Date());
        asnDtlGoods.setCreateUser(HttpSessionUtil.getLoginName(servletRequest));
        asnDtlGoods = asnDtlGoodsService.addNewOne(asnDtlGoods);

        //添加记录：收货通知单明细
        AsnDtl asnDtl = asnRequest.getAsnDtl();
        asnDtl.setCreateTime(new Date());
        asnDtl.setCreateUser(HttpSessionUtil.getLoginName(servletRequest));
        asnDtl = asnDtlService.addNewOne(asnDtl);

        //添加记录：收货通知单信息
        AsnHeader asnHeader = asnRequest.getAsnHeader();
        asnHeader.setCreateTime(new Date());
        asnHeader.setCreateUser(HttpSessionUtil.getLoginName(servletRequest));
        asnHeader.setModifyTime(new Date());
        asnHeader.setModifyUser(HttpSessionUtil.getLoginName(servletRequest));
        asnHeader = asnHeaderService.addNewOne(asnHeader);


        asnRequest.setAsnDtlGoods(asnDtlGoods);
        asnRequest.setAsnDtl(asnDtl);
        asnRequest.setAsnHeader(asnHeader);

        Result<AsnRequest> result = new Result<>();
        result.setData(asnRequest);
        return result;
    }

    /**
     * 更新收货单、收货单明细、收货单明细货品清单(可选)
     *
     * @param asnRequest
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Result<AsnRequest> updateAsn(AsnRequest asnRequest, HttpServletRequest servletRequest) {

        //收货单头
        AsnHeader asnHeader = asnRequest.getAsnHeader();
        asnHeader.setModifyUser(HttpSessionUtil.getLoginName(servletRequest));
        asnHeader.setModifyTime(new Date());
        asnHeader = asnHeaderService.updateOne(asnHeader);

        //收货单明细
        AsnDtl asnDtl = asnRequest.getAsnDtl();
        asnDtl.setModifyTime(new Date());
        asnDtl.setModifyUser(HttpSessionUtil.getLoginName(servletRequest));
        asnDtl = asnDtlService.updateOne(asnDtl);

        //收货单铭心货品清单
        AsnDtlGoods asnDtlGoods = asnRequest.getAsnDtlGoods();
        asnDtlGoods.setModifyTime(new Date());
        asnDtlGoods.setModifyUser(HttpSessionUtil.getLoginName(servletRequest));
        asnDtlGoods = asnDtlGoodsService.updateOne(asnDtlGoods);


        asnRequest.setAsnHeader(asnHeader);
        asnRequest.setAsnDtl(asnDtl);
        asnRequest.setAsnDtlGoods(asnDtlGoods);

        Result<AsnRequest> result = new Result<>();
        result.setData(asnRequest);
        return result;
    }
    
    /**
     * 查询收货报表主档数据
     * @param
     * @return
     */
    public PagerInfo<List<ReceiveReportZhuExt>> searchReceiveReport(
    		ReceiveReportSelectRequest search, Pager<?> pager) {
    	Page<ReceiveReportZhuExt> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
    	
    	List<ReceiveReportZhuExt> datas = asnDtlMapper.searchReceiveReport(search, asnTypeList);
    	
    	int waitCode = QaStatusEnum.WAIT.getCode();
    	int okCode = QaStatusEnum.OK.getCode();
    	int ngCode = QaStatusEnum.NG.getCode();
    	for (ReceiveReportZhuExt ext : datas) {
    		AsnDto asnDto = new AsnDto();
    		asnDto.setAsnDtlId(ext.getAsnDtlId());
    		
    		List<AsnDtlGoods> asnDtlGoodsList = asnDtlGoodsService.getByAsnDtlId(ext.getAsnDtlId());
    		List<String> goodsSnList = asnDtlGoodsList.stream().map(s -> s.getGoodsSn()).collect(Collectors.toList());
    		asnDto.setGoodsSnList(goodsSnList);
    		
    		//待检数
    		List<AsnDtlGoods> waitGoodsList = asnDtlGoodsList.stream().filter(s -> s.getQaStatus().equals(waitCode)).collect(Collectors.toList());
    		asnDto.setWaitCheckCount(waitGoodsList.size());
    		
    		//合格数
    		List<AsnDtlGoods> okGoodsList = asnDtlGoodsList.stream().filter(s -> s.getQaStatus().equals(okCode)).collect(Collectors.toList());
    		asnDto.setQualifiedCount(okGoodsList.size());
    		
    		//不合格数
    		List<AsnDtlGoods> ngGoodsList = asnDtlGoodsList.stream().filter(s -> s.getQaStatus().equals(ngCode)).collect(Collectors.toList());
    		asnDto.setUnQualifiedCount(ngGoodsList.size());
    		ext.setAsnDto(asnDto);
    		
    		// 收货单单据状态
    		ext.setStatusDesc(asnDtlStatusEnumMap.get(ext.getStatus()));
    		
    		// 收单单单据类型
    		ext.setAsnTypeDesc(asnTypeEnumMap.get(ext.getAsnType()));

    		//公司间交货(R09、R10) 订单，显示备用字段1(ASN_REF1)，其他单据类型显示PO_NO字段  by luxm
            if(ext.getAsnType().equals(AsnTypeEnum.R09.code) || ext.getAsnType().equals(AsnTypeEnum.R10.code)){
                ext.setPoNo(ext.getAsnRef1());
            }
    	}
    	
    	PagerInfo<List<ReceiveReportZhuExt>> pagerInfo = new PagerInfo<>();
    	pagerInfo.setTotalCount(page.getTotal());
    	pagerInfo.setItems(datas);
    	return pagerInfo;
    }
    
    /**
     * 查询收货报表明细数据
     * @param
     * @return
     */
    public List<ReceiveReportDtlExt> searchReceiveReportDtl(
    		ReceiveReportDtlRequest search) {
    	return trsnDtlService.selectAll(search.getAsnNo(), search.getAsnLineNo(), 
    			invUpdateFlag, inStorageCode);
    }
    
    /**
     * 查询收货报表SN信息
     *
     * @param request
     * @return
     */
    public PagerInfo<List<ReceiveReportSNExt>> searchReceiveReportSN(
    		ReceiveReportDtlRequest search, Pager<?> pager) {
    	Page<ReceiveReportSNExt> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());

    	//老版本
    	//List<ReceiveReportSNExt> datas = asnDtlGoodsMapper.searchReceiveReportSN(search.getAsnNo(), search.getAsnLineNo());
    	//新版本开始

        List<ReceiveReportSNExt> datas = asnDtlGoodsMapper.searchNewReceiveReportSN(search.getAsnNo(), search.getAsnLineNo());
        if (CollectionUtil.isEmpty(datas)){
            List<ReceiveReportSNExt> nullDatas=new ArrayList<>();
            PagerInfo<List<ReceiveReportSNExt>> nullPagerInfo = new PagerInfo<>();
            nullPagerInfo.setItems(nullDatas);
            return nullPagerInfo;
        }
        PagerInfo<List<ReceiveReportSNExt>> pagerInfo = new PagerInfo<>();
        pagerInfo.setTotalCount(page.getTotal());
        List<String> goodsSnList = datas.stream().map(t -> t.getGoodsSn()).collect(Collectors.toList());
        //查询翻牌库存凭证
        List<TrsnReportExt> trsnReportExts = asnDtlGoodsMapper.
                searchTrsnReportExt(search.getAsnNo(), search.getAsnLineNo(), goodsSnList);
        if (CollectionUtil.isEmpty(trsnReportExts)){
            pagerInfo.setItems(datas);
            return pagerInfo;
        }
        Map<String, List<TrsnReportExt>> trsnExtCollect =
                trsnReportExts.stream().collect(Collectors.groupingBy(TrsnReportExt::getGoodsSn));
        List<TrsnReportExt> extList=new ArrayList<>();
        String fpNoteNo="";
        Integer trsnDtlId=-1;
        for (ReceiveReportSNExt data : datas) {
            if (CollectionUtil.isEmpty(trsnExtCollect.get(data.getGoodsSn()))){
                continue;
            }
            extList=trsnExtCollect.get(data.getGoodsSn());
            for (TrsnReportExt ext : extList) {
                if (ext.getTrsnDtlId()>trsnDtlId){
                    trsnDtlId=ext.getTrsnDtlId();
                    fpNoteNo=ext.getFmNoteNo();
                }
            }
            data.setFpNoteNo(fpNoteNo);
        }

        //新版本结束

        //老版本
    	//PagerInfo<List<ReceiveReportSNExt>> pagerInfo = new PagerInfo<>();
    	//pagerInfo.setTotalCount(page.getTotal());
    	pagerInfo.setItems(datas);
    	return pagerInfo;
    }
    
    /**
     * 导出收货报表
     *
     * @param search
     * @param servletRequest
     * @param servletResponse
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public void exportReceiveReport(
            ReceiveReportSelectRequest search,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        ServletOutputStream out = null;
        XSSFWorkbook workbook = null;
        try {
            List<ReceiveReportZhuExt> datas = asnDtlMapper.searchReceiveReport(search, asnTypeList);

            int waitCode = QaStatusEnum.WAIT.getCode();
            int okCode = QaStatusEnum.OK.getCode();
            int ngCode = QaStatusEnum.NG.getCode();
            for (ReceiveReportZhuExt ext : datas) {
                List<AsnDtlGoods> asnDtlGoodsList = asnDtlGoodsService.getByAsnDtlId(ext.getAsnDtlId());

                //待检数
                List<AsnDtlGoods> waitGoodsList = asnDtlGoodsList.stream().filter(s -> s.getQaStatus().equals(waitCode)).collect(Collectors.toList());
                ext.setWaitCheckCount(waitGoodsList.size());

                //合格数
                List<AsnDtlGoods> okGoodsList = asnDtlGoodsList.stream().filter(s -> s.getQaStatus().equals(okCode)).collect(Collectors.toList());
                ext.setQualifiedCount(okGoodsList.size());

                //不合格数
                List<AsnDtlGoods> ngGoodsList = asnDtlGoodsList.stream().filter(s -> s.getQaStatus().equals(ngCode)).collect(Collectors.toList());
                ext.setUnQualifiedCount(ngGoodsList.size());

                // 收货单单据状态
                ext.setStatusDesc(asnDtlStatusEnumMap.get(ext.getStatus()));

                // 收单单单据类型
                ext.setAsnTypeDesc(asnTypeEnumMap.get(ext.getAsnType()));
            }
            List<ExcelExportConfig> configs = new ArrayList<>();
            configs.add(new ExcelExportConfig("收货单号", "asnNo", 120));
            configs.add(new ExcelExportConfig("来源单号", "sourceNo", 120));
            configs.add(new ExcelExportConfig("工单号", "orderNo", 120));
            configs.add(new ExcelExportConfig("订单号", "poNo", 120));
            configs.add(new ExcelExportConfig("批次号", "lotNo", 120));
            configs.add(new ExcelExportConfig("类型", "asnTypeDesc", 120));
            configs.add(new ExcelExportConfig("销售订单", "saleNo", 120));
            configs.add(new ExcelExportConfig("物料编码", "skuCode", 120));
            configs.add(new ExcelExportConfig("物料描述", "skuDesc", 120));
            configs.add(new ExcelExportConfig("工单数量", "woQty", 120));
            configs.add(new ExcelExportConfig("状态", "statusDesc", 120));
            configs.add(new ExcelExportConfig("计划接收数量", "inspQty", 120));
            configs.add(new ExcelExportConfig("实际收货数量", "rcvQty", 120));
            configs.add(new ExcelExportConfig("合格数", "qualifiedCount", 120));
            configs.add(new ExcelExportConfig("不合格数", "unQualifiedCount", 120));
            configs.add(new ExcelExportConfig("待检数", "waitCheckCount", 120));
            configs.add(new ExcelExportConfig("送检单号", "inspNo", 120));


            workbook = HttpExcel.newListToExcel(datas, configs);
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "收货报表";
            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setContentType("application/octet-stream");
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            out = servletResponse.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if(workbook != null) {
                workbook.close();
            }
        }
    }

    public void exportReceiveReportNew(
            ReceiveReportSelectRequest search,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        ServletOutputStream out = null;
        XSSFWorkbook workbook = null;
        try {
            List<ReceiveReportZhuExt> datas = asnDtlMapper.searchReceiveReport(search, asnTypeList);

            List<Integer> dtlIdList = datas.stream().map(t -> t.getAsnDtlId()).collect(Collectors.toList());

            List<AsnDtlGoods> asnDtlGoodsList=new ArrayList<>();
            List<List<Integer>> dtlIdSplitList = QueryInUtil.splitInParams(999, dtlIdList);
            for (List<Integer> dtlIdSplit : dtlIdSplitList) {
                asnDtlGoodsList.addAll(asnDtlGoodsMapper.selectByDltIds(dtlIdSplit));
            }
            Map<Integer, List<AsnDtlGoods>> dtlGoodsMap = asnDtlGoodsList.stream().collect(Collectors.groupingBy(AsnDtlGoods::getAsnDtlId));

            List<AsnDtlGoods> asnDtlGoodsCheck;
            int waitCode = QaStatusEnum.WAIT.getCode();
            int okCode = QaStatusEnum.OK.getCode();
            int ngCode = QaStatusEnum.NG.getCode();
            int waitCount;//待检数
            int okCount;//合格数
            int ngCount;//不合格数
            for (ReceiveReportZhuExt ext : datas) {
                asnDtlGoodsCheck =dtlGoodsMap.get(ext.getAsnDtlId());

                if (CollectionUtil.isEmpty(asnDtlGoodsCheck)){
                    waitCount=0;okCount=0;ngCount=0;
                }else {
                    waitCount = (int)asnDtlGoodsCheck.stream().filter(s -> s.getQaStatus().equals(waitCode)).count();
                    okCount =(int) asnDtlGoodsCheck.stream().filter(s -> s.getQaStatus().equals(okCode)).count();
                    ngCount = (int) asnDtlGoodsCheck.stream().filter(s -> s.getQaStatus().equals(ngCode)).count();
                }
                ext.setWaitCheckCount(waitCount);
                ext.setQualifiedCount(okCount);
                ext.setUnQualifiedCount(ngCount);



                // 收货单单据状态
                ext.setStatusDesc(asnDtlStatusEnumMap.get(ext.getStatus()));

                // 收单单单据类型
                ext.setAsnTypeDesc(asnTypeEnumMap.get(ext.getAsnType()));
            }
            List<ExcelExportConfig> configs = new ArrayList<>();
            configs.add(new ExcelExportConfig("收货单号", "asnNo", 120));
            configs.add(new ExcelExportConfig("来源单号", "sourceNo", 120));
            configs.add(new ExcelExportConfig("工单号", "orderNo", 120));
            configs.add(new ExcelExportConfig("订单号", "poNo", 120));
            configs.add(new ExcelExportConfig("批次号", "lotNo", 120));
            configs.add(new ExcelExportConfig("类型", "asnTypeDesc", 120));
            configs.add(new ExcelExportConfig("销售订单", "saleNo", 120));
            configs.add(new ExcelExportConfig("物料编码", "skuCode", 120));
            configs.add(new ExcelExportConfig("物料描述", "skuDesc", 120));
            configs.add(new ExcelExportConfig("工单数量", "woQty", 120));
            configs.add(new ExcelExportConfig("状态", "statusDesc", 120));
            configs.add(new ExcelExportConfig("计划接收数量", "inspQty", 120));
            configs.add(new ExcelExportConfig("实际收货数量", "rcvQty", 120));
            configs.add(new ExcelExportConfig("合格数", "qualifiedCount", 120));
            configs.add(new ExcelExportConfig("不合格数", "unQualifiedCount", 120));
            configs.add(new ExcelExportConfig("待检数", "waitCheckCount", 120));
            configs.add(new ExcelExportConfig("送检单号", "inspNo", 120));


            workbook = HttpExcel.newListToExcel(datas, configs);
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "收货报表";
            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setContentType("application/octet-stream");
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            out = servletResponse.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if(workbook != null) {
                workbook.close();
            }
        }
    }

    public void exportReceiveReportNew3(
            ReceiveReportSelectRequest search,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        ServletOutputStream out = null;
        XSSFWorkbook workbook = null;
        try {
            List<ReceiveReportZhuExt> datas = asnDtlMapper.searchReceiveReportNew(search, asnTypeList);

            int waitCode = QaStatusEnum.WAIT.getCode();
            int okCode = QaStatusEnum.OK.getCode();
            int ngCode = QaStatusEnum.NG.getCode();
            for (ReceiveReportZhuExt ext : datas) {
                // 收货单单据状态
                ext.setStatusDesc(asnDtlStatusEnumMap.get(ext.getStatus()));

                // 收单单单据类型
                ext.setAsnTypeDesc(asnTypeEnumMap.get(ext.getAsnType()));
            }
            List<ExcelExportConfig> configs = new ArrayList<>();
            configs.add(new ExcelExportConfig("收货单号", "asnNo", 120));
            configs.add(new ExcelExportConfig("来源单号", "sourceNo", 120));
            configs.add(new ExcelExportConfig("工单号", "orderNo", 120));
            configs.add(new ExcelExportConfig("订单号", "poNo", 120));
            configs.add(new ExcelExportConfig("批次号", "lotNo", 120));
            configs.add(new ExcelExportConfig("类型", "asnTypeDesc", 120));
            configs.add(new ExcelExportConfig("销售订单", "saleNo", 120));
            configs.add(new ExcelExportConfig("物料编码", "skuCode", 120));
            configs.add(new ExcelExportConfig("物料描述", "skuDesc", 120));
            configs.add(new ExcelExportConfig("工单数量", "woQty", 120));
            configs.add(new ExcelExportConfig("状态", "statusDesc", 120));
            configs.add(new ExcelExportConfig("计划接收数量", "inspQty", 120));
            configs.add(new ExcelExportConfig("实际收货数量", "rcvQty", 120));
            configs.add(new ExcelExportConfig("合格数", "qualifiedCount", 120));
            configs.add(new ExcelExportConfig("不合格数", "unQualifiedCount", 120));
            configs.add(new ExcelExportConfig("待检数", "waitCheckCount", 120));
            configs.add(new ExcelExportConfig("送检单号", "inspNo", 120));


            workbook = HttpExcel.newListToExcel(datas, configs);
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "收货报表";
            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setContentType("application/octet-stream");
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            out = servletResponse.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if(workbook != null) {
                workbook.close();
            }
        }
    }
    
    /**
     * 导出收货报表SN信息
     * @param search
     * @param servletRequest
     * @param servletResponse
     * @throws Exception
     */
    public void exportReceiveReportSN(
    		ReceiveReportDtlRequest search,
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	ServletOutputStream out = null;
        XSSFWorkbook workbook = null;        
        try {
        	List<ReceiveReportSNExt> datas = asnDtlGoodsMapper.searchReceiveReportSN(search.getAsnNo(), search.getAsnLineNo());
            for (ReceiveReportSNExt data : datas) {
            	data.setStatusDesc(goodsStatusEnumMap.get(data.getStatus()));
			}
            
            List<ExcelExportConfig> configs = new ArrayList<>();
            configs.add(new ExcelExportConfig("入库凭证", "noteNo", 120));
            configs.add(new ExcelExportConfig("翻牌凭证", "fpNoteNo", 120));
            configs.add(new ExcelExportConfig("条码(SN)", "goodsSn", 120));
            configs.add(new ExcelExportConfig("卡板号", "pkgSn", 120));
            configs.add(new ExcelExportConfig("数量", "qty", 120));
            configs.add(new ExcelExportConfig("状态", "statusDesc", 120));
            configs.add(new ExcelExportConfig("库位编码", "locCode", 120));
            configs.add(new ExcelExportConfig("库位名称", "locDesc", 120));
            configs.add(new ExcelExportConfig("仓库编码", "whsCode", 120));
            configs.add(new ExcelExportConfig("仓库名称", "whsName", 120));
            configs.add(new ExcelExportConfig("是否回传", "isReturn", 120));


            workbook = HttpExcel.newListToExcel(datas, configs);
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "收货单条码(SN)信息";
            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setContentType("application/octet-stream");
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            out = servletResponse.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
			if(workbook != null) {
				workbook.close();
			}
        }
    }

    /**
     * 查询内销和散件
     * @param prdReceiveCode
     * @param udf1
     */
    public List<AsnSaleInDto> selectInSale(String prdReceiveCode, String udf1) {
        List<AsnSaleInDto> asnSaleInDtoList = asnDtlMapper.selectInSale(prdReceiveCode, udf1);
        return asnSaleInDtoList;
    }
}
